/**
 * The base for Multichannel JavaScript
 * requires mc.base.js
 */
 
 ( function( $ ) {
 
 	/* updates eligibility message and store selector link */

	MC.updateSPUMessage = function( ev ){

		/*
		 * relies on MC.updateSPUMessage.init
		 */
		
		var link = this.updateSPUMessage.link,
			message = this.updateSPUMessage.message,
			messageContainer = this.updateSPUMessage.messageContainer,
			href = $(link).attr("href"),
			sku,
			elig;
		
		$.each( ev.target.getSPUEligibility() , function( key , value ){
			if ( key == "sku" ){
				sku =  value.toString();
			} else if ( key == "spuElig" ){
				elig = ( value != undefined ) ? value.toString() : undefined;
			}
		});
		
		if( elig == undefined && $(messageContainer).attr("data-type-ispu-sku") == sku ){	
			updateMessageContainer( { uSku : sku , uElig : false } );
			updateMessageText( message , sku );
		};

		if ( $(messageContainer).attr("data-type-ispu-sku") != sku ){
			updateMessageContainer( { uSku : sku } );	
			if( $(messageContainer).attr("data-type-ispu-elig") != elig ) {	
				if( elig == undefined ){
					updateMessageContainer( { uElig : false } );
				} else {
					updateMessageContainer( { uElig : elig } );
					updateMessageText( message , sku );
				};
			};
			
		};

		function updateMessageContainer( update ){
			if( update.uSku != undefined ){ $(messageContainer).attr( "data-type-ispu-sku" , update.uSku ) };
			if( update.uElig != undefined ){ $(messageContainer).attr( "data-type-ispu-elig" , update.uElig ) }
				else if ( update.uElig == false )  { $(messageContainer).attr( "data-type-ispu-elig" , "false" ) };
		};
	
		function updateMessageText( obj , sku ){
			var html = $(obj).html().trim(),
				cssClass = $(obj).parent("li").attr("class");
			$(obj).html( $(obj).attr("data-type-ispu-message") ).attr( "data-type-ispu-message" ,  html );
			$(obj).parent().attr( "class" , $(obj).parent().attr("data-type-ispu-alt-class") ).attr( "data-type-ispu-alt-class" , cssClass );
			( $(obj).parent().attr("data-type-ispu-elig") == "true" && $(link).attr("data-type-default-show-link") == "true" ) ? updateLink("show" , sku ) : updateLink();
		};
		
		function updateLink( show , sku ){		
			( show == "show") ? updateLinkHref(sku , function(){ $(link).fadeIn(450).addClass("active") } ) : $(link).fadeOut(250).removeClass("active");
		};

		function updateLinkHref( sku , callback ){
			// search for these -
				// ProductSKU=
				// &SKU=
			var oldsku = href.slice( href.indexOf("ProductSKU=") + 11 , href.indexOf("&SKU=") );
				newHref = href.replace( new RegExp( oldsku, 'g') , sku );
			$(link).attr("href" , newHref );
			//callback
			return callback ? callback.apply() : false
		};

	};
	
	MC.updateSPUMessage.getMessageElements = function( callback ){
		MC.updateSPUMessage.messageContainer = $("div.availability-messaging ul li.mc-ispu");	
		MC.updateSPUMessage.message = $(MC.updateSPUMessage.messageContainer).children("span");
		MC.updateSPUMessage.link = $(MC.updateSPUMessage.messageContainer).children("a");
		return callback ? callback.apply() : false
	};

	/* get eligibility added to KOR - kor.product.js */
	KOR.Product.prototype.getSPUEligibility = function(){
		return { spuElig : this.getCartableProductVariant() ? this.getCartableProductVariant().spuEligibility: undefined , sku : this.getBestVariantSKU() }
	};		

	/* observer added to KOR - kor.product-view.js */
	KOR.ProductView.prototype.registerObservers = KOR.ProductView.prototype.registerObservers.overload( function(){
		this._super.apply( this , arguments );
		this.observe('changeAttributeComplete', this.changeAttributeCompleteSPUEligibilityHandler, this )
	});	
	
	/* handlers added to KOR - kor.product-view.js */
	KOR.ProductView.prototype.changeAttributeCompleteSPUEligibilityHandler = function( ev ){
		if( MC.updateSPUMessage ){
			MC.updateSPUMessage.getMessageElements( function(){ MC.updateSPUMessage( ev ) });
		}
	};
	
	/* END MC.updateSPUMessage */
 
 })( jQuery );